package com.marvels.approve.process.listener;

import org.activiti.engine.TaskService;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.ExecutionListener;
import org.activiti.engine.task.Task;
import org.activiti.engine.task.TaskQuery;
import org.springframework.stereotype.Component;

import com.marvels.approve.common.utils.ApplicationContextAchieve;
import com.marvels.approve.process.dto.CompleteProcessDTO;
import com.marvels.approve.process.dto.ProcessApproveDTO;
import com.marvels.approve.process.service.IProcessService;
import com.marvels.common.constants.MarvelsConstant;
import com.marvels.common.exception.MarvelsBusinessException;
import com.marvels.common.utils.MarvelsLogUtil;

/**
 * 审批复核监听器
 * 
 * @author tangpw
 *
 */
@Component
public class CreditThirdStageListen implements ExecutionListener {

	private static final long serialVersionUID = 1L;
 
	/**
	 * 跑决策
	 * @param loanId
	 * @return false
	 * @throws MarvelsBusinessException 
	 */
	public Boolean runningDecision(String loanId) throws MarvelsBusinessException {
//		ILoanDecisionService loanDecisionService = ApplicationContextAchieve.getApplicationContext().getBean(ILoanDecisionService.class); 
//		return loanDecisionService.runCreditDecision(loanId);
		return Boolean.TRUE;
	}

	@Override
	public void notify(DelegateExecution execution) throws Exception {
		try {
			boolean isPass = false;
			TaskService taskService=ApplicationContextAchieve.getApplicationContext().getBean(TaskService.class);
			IProcessService processService=ApplicationContextAchieve.getApplicationContext().getBean(IProcessService.class);
			String processInstanceId = execution.getProcessInstanceId();
			TaskQuery tq = taskService.createTaskQuery().processInstanceId(processInstanceId);
			Task task = tq.singleResult();
			Object processResult = taskService.getVariable(task.getId(),"processResult");
			//非成功不更改审批条件
			if(!MarvelsConstant.PROCESS_RESULT_OK.equals(processResult)){
				return ;
			}
			
			String curNodeCode = task.getTaskDefinitionKey();
			String curNodeName = task.getName();
			String curTaskId = task.getId();
			Object object = taskService.getVariable(task.getId(),"loanId");
			String loanId = "";
			if (null != object) {
				loanId =String.valueOf(object);
			}
			// 跑决策
			try {
				isPass = runningDecision(loanId);
			} catch (Exception e) {
				MarvelsLogUtil.getInstance().error(">>>>>>>loanId" + loanId + ",征信前置阶段跑决策异常:", e);
				throw new MarvelsBusinessException("征信前置阶段跑决策异常");
			}
			CompleteProcessDTO proDto = new CompleteProcessDTO();
			proDto.setLoanId(loanId);
			MarvelsLogUtil.getInstance().info(loanId+"跑决策结果："+isPass+">>>>>>>>>>>>>征信3阶段监听 curNodeCode:" + curNodeCode + ",curNodeName"
					+ curNodeName + ",curTaskId" + curTaskId);
//		ActivitiNodeDTO curNode = new ActivitiNodeDTO();
//		curNode.setNodeCode(CaseNodeConstant.WOEXCLUSIVEGATEWAY5.getCode());
			// 决策通过 征信审核 
			ProcessApproveDTO processApproveDto = new ProcessApproveDTO();
			if (isPass) {
				processApproveDto.setProcessResult(MarvelsConstant.PROCESS_RESULT_OK);
				// 设置当前节点
//			proDto.setCurNode(curNode);
			}  
//		taskService.setVariables(task.getId(), BeanUtils.beanConverToMap(processApproveDto));
			taskService.setVariable(task.getId(), "processResult", processApproveDto.getProcessResult());
			taskService.setVariable(task.getId(), "loanId", loanId);
			processResult = taskService.getVariable(task.getId(),"processResult");
			MarvelsLogUtil.getInstance().info(loanId+">>>>>>>测试日志processResultprocessResultprocessResultprocessResult"+processResult);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("征信前置阶段跑决策流转异常",e);
			throw e;
		}
 
		
	}
}
